package org.bangumibuddy.security.controller;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.bangumibuddy.config.property.JwtProperties;
import org.bangumibuddy.dto.UserDto;
import org.bangumibuddy.pojo.ResponseResult;
import org.bangumibuddy.pojo.token.TokenData;
import org.bangumibuddy.security.service.BangumiUserDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@Api("用户注册登陆模块")
public class AuthenticationController {

    @Autowired
    BangumiUserDetailService bangumiUserDetailService;

    @Autowired
    JwtProperties jwtProperties;

    @PostMapping("/user/login")
    public ResponseResult<TokenData> login(@RequestBody UserDto userDto) throws Exception {
        //验证账号密码
        log.trace("用户登录：{}",userDto);
        TokenData tokens = bangumiUserDetailService.ValidateUser(userDto);

        //返回token
        if(tokens == null)
            return ResponseResult.error("密码错误！");

        return ResponseResult.success(tokens);
    }
}
